package com.chixing.service.impl;

import com.chixing.mapper.CustomerMapper;
import com.chixing.pojo.Customer;
import com.chixing.pojo.CustomerExample;
import com.chixing.service.RegisterService;
import com.chixing.util.MD5Utils;
import com.chixing.util.ServerResponse;
import org.mindrot.jbcrypt.BCrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RegisterServiceImpl implements RegisterService {
    @Autowired
     private CustomerMapper customerMapper;
    @Override
    public ServerResponse add(Customer customer) {
        Long custTel = customer.getCustTel();
        CustomerExample customerExample = new CustomerExample();
        customerExample.createCriteria().andCustTelEqualTo(custTel);
      List<Customer> customerList = customerMapper.selectByExample(customerExample);
        Customer customer1=null;
        if(customerList.size()>0)
            customer1  = customerList.get(0);
        int rows = 0;
      if(customer1==null) {
          System.out.println("注册的密码"+customer.getCustPassword());
          String password = MD5Utils.formPassToDBPass(customer.getCustPassword(),"1a2b3c4d");
          customer.setCustPassword(password);
          System.out.println("最终持久化到数据库中的密码是：" +password );
          rows = customerMapper.insert(customer);
      }
        if(rows>0){
            return ServerResponse.saveSuccess(rows);
        }else {
            return ServerResponse.saveFail(null);
        }
    }

    @Override
    public ServerResponse getCustTel(Long custTel,String custPassword) {
        CustomerExample example = new CustomerExample();
        example.createCriteria().andCustTelEqualTo(custTel).andCustPasswordEqualTo(custPassword);
      List<Customer> customerList = customerMapper.selectByExample(example);
      if(customerList.size()>0){
          Customer customer =customerList.get(0);
          return ServerResponse.saveSuccess(customer);
      }else {
          return ServerResponse.saveFail(null);
      }
    }

//    @Override
//    public void regist(Long custTel, String custPassword) {
//        System.out.println("service custPwd:" + custPassword);
//
//        String newPasswordToDB = MD5Utils.formPassToDBPass(custPassword,"1a2b3c4d");
//        System.out.println("最终持久化到数据库中的密码是：" +newPasswordToDB );
//
//    }

}
